<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_daftar_ulang extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function get_calon_siswa($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'no_pendaftaran';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$id_psb = isset($_POST['id_psb']) ? strval($_POST['id_psb']) : '';
		$id_kelompok = isset($_POST['id_kelompok']) ? strval($_POST['id_kelompok']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.id, a.no_pendaftaran, a.nama, a.jk');
			$this->db->from('tbl_sd_calonsiswa a');
			
			#filter
			$this->db->where('a.id_psb', $id_psb);
			$this->db->where('a.id_kelompok', $id_kelompok);
			$this->db->where('a.status', '2');
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			if($page != 0)
				$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function get_data_daftar_ulang($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 1000;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'no_pendaftaran';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$id_psb = isset($_POST['id_psb']) ? strval($_POST['id_psb']) : '';
		$id_kelompok = isset($_POST['id_kelompok']) ? strval($_POST['id_kelompok']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.id, a.no_pendaftaran, a.nama, a.jk, a.status');
			$this->db->from('tbl_sd_calonsiswa a');
			// $this->db->join('tbl_sd_psb b', 'b.id_psb=a.id_psb');
			// $this->db->join('tbl_sd_psb_kelompok c', 'c.id_kelompok=a.id_kelompok');
			
			#filter
			//if($id_psb != '')
				$this->db->where('a.id_psb', $id_psb);
			//if($id_kelompok != '')
				$this->db->where('a.id_kelompok', $id_kelompok);
				
			$this->db->where('a.status', '1');
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			if($page != 0)
				$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function get_data_edit($id){
		$this->db->flush_cache();
		$this->db->select('a.id, a.no_pendaftaran, a.nama, b.nama_psb, c.nama_kelompok, a.status');
		$this->db->from('tbl_sd_calonsiswa a');
		$this->db->join('tbl_sd_psb b', 'b.id_psb=a.id_psb');
		$this->db->join('tbl_sd_psb_kelompok c', 'c.id_kelompok=a.id_kelompok');
		$this->db->where('id', $id);
		
		return $this->db->get();
	}
	
	function togrid($data, $count){
		$response->total = $count;
		$response->rows = array();
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}
		
		return json_encode($response);
	}
	
	function save_daftar_ulang($data){
		$this->db->trans_start();
		
		foreach($data['data']['rows'] as $row){
			$this->db->set('status', $row['status']==1?'2':'3');
			$this->db->where('id', $row['id']);
			$this->db->update('tbl_sd_calonsiswa');
		}
		
		//return
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}
	
	function edit_status($data){
		$this->db->flush_cache();
		$this->db->set('status', $data['status']==1?'2':'3');
		$this->db->where('id', $data['id']);
		
		return $this->db->update('tbl_sd_calonsiswa');
	}
	
	function get_nama_siswa_by_id($id){
		$this->db->flush_cache();
		$this->db->select('nama');
		$this->db->where('id', $id);
		return $this->db->get('tbl_sd_calonsiswa');
	}
	
}

?>